class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            pwd: '',
            phone: '',
            //  img: ['./src/t1.png', './src/t2.png'],
            img: ['', ''],
            idcard: '',
            name: '',
            hassend: !0,
            district: [
                ''
            ],
        };
    }








    componentDidMount() {
        if (Constant.SWITCH_VCONSOLE) {
            new VConsole();
        }
        根据id获取idv(1, res => {
            if (res.issuccess == 1) {
                let hassend = !1;
                if (res.data) {
                    hassend = !0

                }
                this.setState({ hassend: !0 })
            }
        })

        // let openid = localStorage.getItem('openid')
        // getuser(openid, res => {
        //     if (res.issuccess == 1) {
        //         let user = res.user
        //         this.setState({ user, allprice:835, amount:  835 })
        //     }
        // })

        // let sizehtml = [],cphtml=[], { sizelist } = this.state;

        // sizehtml.push(<option value={-1}>请选择</option>)
        // sizelist.forEach((x, i) => {
        //     sizehtml.push(
        //         <option key={i} value={x}>{x} </option>
        //     )
        // })
        // cphtml.push(<option value={1}>大包(￥535元)</option>)
        // cphtml.push(<option value={0}>小包(￥310元)</option>)
        // cphtml.push(<option value={-1}>不购买</option>)

        // this.setState({ sizehtml,openid,cphtml })

        //  console.log(liststr?'1':'2','liststr');
        // let list = JSON.parse(localStorage.getItem('orderlist')) || []
        // console.log(list, 'list');

        // this.setState({ list }, () => {
        //     this.getamount()
        // })
        // this.getdata()

    }

    pay(size) {
        let { openid, amount } = this.state;
        praypay(openid, Number(amount) * 100, size, res => {
            console.log(res, 'resres');
            let data = JSON.parse(res.ret)
            this.requestpay(data)
        })

    }

    requestpay(data) {
        function onBridgeReady() {

            let sign = md5(`appId=wxa29d77bb2e0c40bb&nonceStr=${data.nonce_str}&package=${data.package}&signType=MD5&timeStamp=${data.timeStamp}&key=Y3A7lWq2Xsd7ScCTSp8QUqLiWELxHCl7`).toUpperCase()
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                "appId": "wxa29d77bb2e0c40bb",     //公众号ID，由商户传入     
                "timeStamp": data.timeStamp,         //时间戳，自1970年以来的秒数     
                "nonceStr": data.nonce_str, //随机串     
                "package": data.package,
                "signType": "MD5",         //微信签名方式：     
                "paySign": sign //微信签名 
            },

                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {

                        // 使用以上方式判断前端返回,微信团队郑重提示：
                        //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    }
                });
        }
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }
    }



    inputbz(e) {
        this.setState({ desc: e.target.value })
    }

    clear() {
        var b = confirm('确定要清空吗')
        if (b) {
            localStorage.removeItem('orderlist')
            this.setState({ list: [], desc: '', amount: 0 })
        }
    }

    gopay() {
        let { sizeid, openid } = this.state;
        if (sizeid == '' || sizeid == '请选择') {
            alert('请先选择尺寸')
        } else {

            canbuy(openid, res => {
                if (res.data == true) {
                    this.pay(sizeid)
                } else {
                    alert('请勿重复购买')
                }

            })
            // 
        }

    }
    toxieyi(e) {
        window.location.href = './xieyi.html?t=' + e
    }

    selectchange(e) {
        let { value } = e.target
        this.setState({ sizeid: value })
    }
    addimg() {
        $('#fileimgsrc').click()
    }

    filechangeimg(e) {
        var that = this, { img } = this.state;
        var filedata = $('#fileimgsrc')[0].files[0];
        console.log(filedata, 'filedata');
        let _name, _fileName, filedatasize = 2048;

        _name = $('#fileimgsrc')[0].value;
        _fileName = _name.substring(_name.lastIndexOf(".") + 1).toLowerCase();
        if (_fileName !== "png" && _fileName !== "jpg" && _fileName !== "gif" && _fileName !== "jpeg") {
            alert("上传图片格式不正确，请重新上传");
        } else {
            if (filedatasize < filedata.size / 1024) {
                alert("上传图片不能大于2M");
                return;
            }

            // let filee = e
            var reader = new FileReader();
            reader.readAsDataURL(filedata);
            reader.onload = function (e) {
                var base64str = e.target.result;
                console.log('base64str', base64str);
                //  img.push(base64str)
                if (img[0] == '') {
                    img[0] = base64str
                } else {
                    img[1] = base64str
                }
                that.setState({ img })
                //  that.putswiperimglistdata([base64str], filee)
            }
        }
    }
    del_img(index) {
        console.log('del_img', index);
        let { img } = this.state
        img[index] = ''
        this.setState({ img })
    }


    cbchange(e) {
        //console.log('cbchange', e.target.checked);
        this.setState({ isagree: e.target.checked })
    }
    inputchange(type, e) {
        console.log('inputchange', { type, e });
        //  let { value } = e.target
        this.setState({ [type]: e.target.value })
    }

    selectchange1(e) {
        let { value } = e.target, { amount, allprice } = this.state;
        if (value == 0) {
            amount = 610
        } if (value == -1) {
            amount = 300
        } if (value == 1) {
            amount = 835
        }
        this.setState({ cpid: value, amount })
    }
    sumbit() {
        let { name, idcard, img } = this.state, err = '';
        console.log({ img, name, idcard });
        if (idcard.length != 18) err = '身份证号输入有误'
        if (name == '') err = '姓名输入有误'
        if (!isBase64(img[0]) || !isBase64(img[1])) err = '请上传身份证图片'

        let data = { name, idcard, userid: 1 }
        uploadimg(img[0], res => {
            if (res.issuccess == 1) {
                data['srcfront'] = res.path
                uploadimg(img[1], res1 => {
                    if (res1.issuccess == 1) {
                        data['srcback'] = res.path
                        idvadd(data, res => {
                            if (res.issuccess == 1) {
                                alert('提交成功')
                            }
                        })
                    }
                })
            }
        })



        // if(err!=''){
        //     alert(err)
        // }else{


        // }

    }


    render() {
        let { name, idcard, img, hassend, sizeid, cphtml, cpid } = this.state;
        //console.log(sizehtml, 'sizehtml');
        return (
            <div className="container" >
                <div className="title">实名认证</div>
                <div style={{ display: hassend ? 'none' : '' }}>
                    <div class="app">
                        <div className="d1">
                            <p>账户</p>
                            <input type="text" placeholder="请输入姓名" value={name} onChange={this.inputchange.bind(this, 'name')} />
                        </div>
                        <div className="d1">
                            <p>身份证号</p>
                            <input type="text" placeholder="请输入身份证号" maxLength={18} value={idcard} onChange={this.inputchange.bind(this, 'idcard')} />
                        </div>


                    </div>

                    <div class="app">
                        <div className="d1">
                            <p>请上传身份证（正反两张）</p>
                        </div>
                        <div className="d1 img">
                            <img onClick={this.addimg.bind(this)} style={{ display: img[0] == '' || img[1] == '' ? "" : "none" }} src='./src/add.png' className="addimg" />
                            <input onChange={this.filechangeimg.bind(this)} type='file' id='fileimgsrc' style={{ display: "none" }} />
                            <div className="img_warp imgsrc" style={{ display: img[0] == '' ? "none" : "" }}>
                                <img src={img[0]} className="img" />
                                <img className='closeimg' onClick={this.del_img.bind(this, '0')} src='./src/img_close.png' />
                            </div>
                            <div className="img_warp imgsrc" style={{ display: img[1] == '' ? "none" : "" }}>
                                <img src={img[1]} className="img" />
                                <img className='closeimg' onClick={this.del_img.bind(this, '1')} src='./src/img_close.png' />
                            </div>

                        </div>


                    </div>

                    <div onClick={this.sumbit.bind(this)} className="submit">提交 </div>
                </div>
                <div className="check" style={{ display: hassend ? '' : 'none' }}>
                    <img src="./src/shenhezhong.png" alt="" />
                    <p>材料已提交，审核中，请耐心等待</p>
                    <p onClick={()=>{
                        window.location.href='./mine.html'
                    }} className="btn">返回</p>
                    
                    
                </div>
            </div>

        )
    }
}

ReactDOM.render(
    <div>
        {/* <TopLine /> */}
        < div id='body' >
            {/* <Tabbar index='1' /> */}
            < Main />
        </div >
    </div >
    // <Barrage />
    , $('.container')[0]
)